package tool

import "strings"

func IsValid(key string) bool {
	stack := make([]rune, 0, 2)
	for _, val := range key {
		if val == '{' || val == '}' {
			if len(stack) == 0 && val == '{' {
				stack = append(stack, val)
			} else if len(stack) == 1 && val == '}' {
				stack = append(stack, val)
			} else {
				return false
			}
		}
	}
	if len(stack) == 1 {
		return false
	}
	return true
}

func Between(str string) string {

	s := strings.Index(str, "{")

	if s < 0 {

		return ""

	}

	s += len("{")

	e := strings.Index(str[s:], "}")

	if e < 0 {

		return ""

	}

	return str[s : s+e]

}

func Replace(str, old, new string) string {
	old = "{" + old + "}"
	return strings.Replace(str, old, new, 1)
}
